<?php
declare(strict_types=1);

namespace App\Library\Crawl;

use App\Library\SnowFlakeId;
use Illuminate\Support\Facades\DB;
use phpspider\core\requests;
use phpspider\core\selector;
use zgldh\QiniuStorage\QiniuStorage;

class BcoderssCrawl
{
    public function crawl(string $url)
    {
        var_dump($this->realCrawl($url, "482398959034175122", "482398959034175122", "481835188276430887", "504122990246497577"));
    }

    /**
     * 真实爬取地址
     * @param string $url 请求地址
     * @param string $userUid 用户uid
     * @param string $authorUid 作者uid
     * @param string $seriesUid 壁纸系列
     * @param string $categoryUid 壁纸分类
     * @return array
     */
    private function realCrawl(string $url, string $userUid, string $authorUid, string $seriesUid, string $categoryUid): array
    {
        $html = requests::get($url);
        $data = selector::select($html, "//ul[contains(@class,'wallpaper')]");
        // 正则表达式
        $pattern = '/<img.*?src="(.*?)".*?alt="(.*?)".*?>/i';
        // 匹配图片标签
        preg_match_all($pattern, (string)$data, $matches);
        $imageAllList = [];
        // 输出结果
        foreach ($matches[1] as $key => $value) {
            $imageAllList[] = [
                "image" => $value,
                "title" => html_entity_decode($matches[2][$key], ENT_QUOTES, 'UTF-8'),
            ];
        }
        var_dump($imageAllList);
        $image = [];
        $imageItem = [];
        foreach ($imageAllList as $key => $value) {
            $disk = QiniuStorage::disk('qiniu');
            $fileName = md5(SnowFlakeId::getId()) . '.' . pathinfo($value["image"])["extension"];
            $bool = $disk->put($fileName, file_get_contents($value["image"]));
            if ($bool) {
                $url = env("QINIU_URL");
                $imageUid = SnowFlakeId::getId($key, 1024);
                // 壁纸系列
                $image[] = [
                    "uid" => $imageUid,
                    "user_uid" => $userUid,
                    "author_uid" => $authorUid,
                    "series_uid" => $seriesUid,
                    "category_uid" => $categoryUid,
                    "item_count" => count($imageAllList),
                    "url" => $url,
                    "path" => $fileName,
                    "title" => $value["title"],
                    "download" => mt_rand(1000, 100000),
                    "collect" => mt_rand(1000, 100000),
                    "created_at" => date("Y-m-d H:i:s"),
                    "updated_at" => date("Y-m-d H:i:s"),
                    "is_show" => 1,
                ];
                // 壁纸集合
                $imageItem[] = [
                    "uid" => SnowFlakeId::getId(),
                    "user_uid" => $userUid,
                    "image_uid" => $imageUid,
                    "author_uid" => $authorUid,
                    "url" => $url,
                    "path" => $fileName,
                    "created_at" => date("Y-m-d H:i:s"),
                    "updated_at" => date("Y-m-d H:i:s"),
                    "is_show" => 1,
                ];
                DB::table("image")->insert($image);
                DB::table("image_item")->insert($imageItem);
            }
        }
        return [$image, $imageItem];
    }
}
